package zzl.com;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws IOException {
       String uri = "hdfs://192.168.137.100:9000/my.txt";
        Configuration con = new Configuration();
        con.set("mapreduce.app-submission.cross-platform", "true");
        con.set("dfs.permissions", "false");
        System.setProperty("hadoop.home.dir", "E:\\hadoop-2.7.5");
        FileSystem fileSystem = FileSystem.get(URI.create(uri),con);
        FSDataInputStream in = null;
        in = fileSystem.open(new Path(uri));
        IOUtils.copyBytes(in,System.out,4096,false);
        IOUtils.closeStream(in);

        boolean b = fileSystem.exists(new Path("/hello"));
        System.out.println(b);

        boolean success = fileSystem.mkdirs(new Path("/zzl"));
        System.out.println(success);

        success = fileSystem.delete(new Path("/zzl"), true);
        System.out.println(success);

        FSDataOutputStream out = fileSystem.create(new Path("/test.data"), true);
        FileInputStream fis = new FileInputStream("C:/Users/EDZ/Desktop/工作目录/zzl.txt");
        IOUtils.copyBytes(fis, out, 4096, true);

        FileStatus[] statuses = fileSystem.listStatus(new Path("/"));
        //System.out.println(statuses.length);
        for(FileStatus status : statuses) {
            System.out.println(status.getPath());
            System.out.println(status.getPermission());
            System.out.println(status.getReplication());
        }
    }
}
